const axios = require('axios');

// 测试配置
const BASE_URL = 'http://localhost:3000/api/v1';
const testUser = {
  username: 'user',
  password: '123456'
};

// 登录函数
async function login() {
  try {
    console.log(`正在登录用户: ${testUser.username}`);
    const response = await axios.post(`${BASE_URL}/auth/login`, testUser);
    
    if (response.data.success && response.data.data && response.data.data.tokens && response.data.data.tokens.accessToken) {
      console.log('登录成功！');
      console.log('用户信息:', response.data.data.user);
      return {
        success: true,
        token: response.data.data.tokens.accessToken,
        user: response.data.data.user
      };
    } else {
      console.error('登录响应中未找到accessToken');
      return { success: false, error: '登录响应中未找到accessToken' };
    }
  } catch (error) {
    console.error('登录失败:', error.message);
    return { success: false, error: error.message };
  }
}

// 测试获取用户收藏的视频
async function testGetUserFavorites(token) {
  try {
    console.log('\n测试获取用户收藏的视频...');
    const response = await axios.get(`${BASE_URL}/users/me/favorites/videos`, {
      headers: {
        Authorization: `Bearer ${token}`
      }
    });
    
    console.log('获取收藏视频成功！');
    console.log('响应状态:', response.status);
    console.log('收藏视频总数:', response.data.data.pagination.total);
    console.log('当前页视频数量:', response.data.data.videos.length);
    console.log('分页信息:', response.data.data.pagination);
    
    if (response.data.data.videos.length > 0) {
      console.log('前3个收藏视频信息:');
      response.data.data.videos.slice(0, 3).forEach((video, index) => {
        console.log(`${index + 1}. ID: ${video.id}, 标题: ${video.title}, 用户ID: ${video.user_id}`);
      });
    }
    
    return { success: true, data: response.data };
  } catch (error) {
    console.error('获取收藏视频失败:', error.response?.data || error.message);
    return { success: false, error: error.response?.data || error.message };
  }
}

// 测试获取用户发布的视频
async function testGetUserVideos(token) {
  try {
    console.log('\n测试获取用户发布的视频...');
    const response = await axios.get(`${BASE_URL}/users/me/videos`, {
      headers: {
        Authorization: `Bearer ${token}`
      }
    });
    
    console.log('获取发布视频成功！');
    console.log('响应状态:', response.status);
    console.log('发布视频总数:', response.data.data.pagination.total);
    console.log('当前页视频数量:', response.data.data.videos.length);
    console.log('分页信息:', response.data.data.pagination);
    
    if (response.data.data.videos.length > 0) {
      console.log('前3个发布视频信息:');
      response.data.data.videos.slice(0, 3).forEach((video, index) => {
        console.log(`${index + 1}. ID: ${video.id}, 标题: ${video.title}, 发布时间: ${video.created_at}`);
      });
    }
    
    return { success: true, data: response.data };
  } catch (error) {
    console.error('获取发布视频失败:', error.response?.data || error.message);
    return { success: false, error: error.response?.data || error.message };
  }
}

// 测试获取用户点赞的视频
async function testGetUserLikedVideos(token) {
  try {
    console.log('\n测试获取用户点赞的视频...');
    const response = await axios.get(`${BASE_URL}/users/me/likes/videos`, {
      headers: {
        Authorization: `Bearer ${token}`
      }
    });
    
    console.log('获取点赞视频成功！');
    console.log('响应状态:', response.status);
    console.log('点赞视频总数:', response.data.data.pagination.total);
    console.log('当前页视频数量:', response.data.data.videos.length);
    console.log('分页信息:', response.data.data.pagination);
    
    if (response.data.data.videos.length > 0) {
      console.log('前3个点赞视频信息:');
      response.data.data.videos.slice(0, 3).forEach((video, index) => {
        console.log(`${index + 1}. ID: ${video.id}, 标题: ${video.title}, 用户ID: ${video.user_id}`);
      });
    }
    
    return { success: true, data: response.data };
  } catch (error) {
    console.error('获取点赞视频失败:', error.response?.data || error.message);
    return { success: false, error: error.response?.data || error.message };
  }
}

// 主测试函数
async function runTests() {
  console.log('开始测试用户收藏和点赞功能...');
  
  // 1. 登录
  const loginResult = await login();
  if (!loginResult.success) {
    console.error('测试失败: 无法登录');
    process.exit(1);
  }
  
  const { token, user } = loginResult;
  
  // 2. 测试获取用户发布的视频
  const videosResult = await testGetUserVideos(token);
  if (!videosResult.success) {
    console.error('测试失败: 获取发布视频失败');
    process.exit(1);
  }
  
  // 3. 测试获取用户收藏的视频
  const favoritesResult = await testGetUserFavorites(token);
  if (!favoritesResult.success) {
    console.error('测试失败: 获取收藏视频失败');
    process.exit(1);
  }
  
  // 4. 测试获取用户点赞的视频
  const likedVideosResult = await testGetUserLikedVideos(token);
  if (!likedVideosResult.success) {
    console.error('测试失败: 获取点赞视频失败');
    process.exit(1);
  }
  
  console.log('\n所有测试通过！');
  process.exit(0);
}

// 运行测试
runTests().catch(error => {
  console.error('测试过程中发生未预期错误:', error);
  process.exit(1);
});